/***********************************************************************
	filename: 	DaterEventFactoryManager.h
	created:	14/04/2012
	author:		4Ys
	
	purpose:	Defines interface for EventFactoryManager class
*************************************************************************/
#ifndef _DaterEventFactoryManager_h_
#define _DaterEventFactoryManager_h_

#include "DaterSingleton.h"
#include "DaterEventFactory.h"

#include <map>
#include <vector>

namespace Dater
{
	/*!
	\brief
		Class that manages EventFactory objects.
	*/
	class EventFactoryManager : public Singleton<EventFactoryManager>
	{
	public:
		EventFactoryManager(void);

		~EventFactoryManager(void){}

		void addFactory(EventFactory* factory);

		template <typename T>
		static void addFactory();

		void removeFactory(const std::string& name);

		void removeFactory(EventFactory* factory);

		void removeAllFactories(void);

		EventFactory* getFactory(const std::string& type) const;

		bool isFactoryPresent(const std::string& name) const;

	private:
		typedef std::map<std::string, EventFactory*> EventFactoryRegistry;
		typedef std::vector<EventFactory*> OwnedEventFactoryList;

		EventFactoryRegistry d_factoryRegistry;
		static OwnedEventFactoryList d_ownedFactories;
	};

	//----------------------------------------------------------------------------//
	template<typename Ta>
	void EventFactoryManager::addFactory()
	{
		// create the factory object
		EventFactory* factory = new Ta;

		// only do the actual add now if our singleton has already been created
		if (EventFactoryManager::getSingletonPtr())
		{
			// add the factory we just created
			EventFactoryManager::getSingleton().addFactory(factory);
		}

		d_ownedFactories.push_back(factory);
	}

}

#endif